//
//  NetworkVC.swift
//  SwfitDemo
//
//  Created by menchao on 2018/9/27.
//  Copyright © 2018年 Test. All rights reserved.
//

import UIKit
import Alamofire

class NetworkVC: UIViewController {
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.addSubview(self.getButton)
        self.view.addSubview(self.postButton)
        self.getButton.snp.makeConstraints { (make) in
            make.left.equalTo(self.view).offset(30)
            make.top.equalTo(self.view).offset(120)
        }
        self.postButton.snp.makeConstraints { (make) in
            make.left.equalTo(self.view).offset(30)
            make.top.equalTo(self.getButton.snp.bottom).offset(20)
            make.width.equalTo(100)
        }
    }
    
    @objc func clickMe(){
        
        var header:HTTPHeaders = [:]
        if let authorizationHeader = Request.authorizationHeader(user: "xxxx", password: "xxxxxx") {
            header[authorizationHeader.key] = authorizationHeader.value
        }
        
        
        Alamofire.request("https://httpbin.org/get", method: .get, parameters: ["key1":"value1","key2":"value2"], encoding: URLEncoding.default, headers: header).responseJSON { (response) in
            if(response.error == nil){
                print("请求成功")
                
                print(response.result.value!)
            }else{
                print("请求失败\(String(describing: response.error))")
            }
        }
        
    }
    
    @objc func clickMeForPost(){
        Alamofire.request("https://httpbin.org/post", method: .post, parameters: ["key1":"value1","key2":"value2"], encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
            if(response.error == nil){
                print("请求成功")
                
                print(response.result.value!)
            }else{
                print("请求失败\(String(describing: response.error))")
            }
        }
        
    }
    
    
    lazy var getButton: UIButton = {
        let button = UIButton()
        button.setTitle("获取Get", for: UIControlState.normal)
        button.backgroundColor = UIColor.green
        button.addTarget(self, action: #selector(clickMe), for: UIControlEvents.touchUpInside)
        return button
    }()
    
    
    lazy var postButton: UIButton = {
        let button = UIButton()
        button.setTitle("获取Post", for: UIControlState.normal)
        button.backgroundColor = UIColor.green
        button.addTarget(self, action: #selector(clickMeForPost), for: UIControlEvents.touchUpInside)
        return button
    }()
}
